package com.kasax.android.arch.base.ui

import android.view.MenuItem
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding


abstract class BaseBindingActivity<V : ViewDataBinding> : BaseCompatActivity() {
    public open var mDataBinding: V? = null

    // toolbar是否支持返回
    protected var isToolbarSupportReback: Boolean = true

    override fun injectBeforeSetContentView() {
        super.injectBeforeSetContentView()
        mDataBinding = DataBindingUtil.setContentView(this, getContentLayoutId())
    }

    override fun needAutoSetContentView(): Boolean = false

    override fun initView() {
        isToolbarSupportReback = needSupportReback()
        if (isToolbarSupportReback) {
            supportActionBar?.let {
                it.setHomeButtonEnabled(true)
                it.setDisplayHomeAsUpEnabled(true)
            }
        }
    }

    /**
     * 不想支持返回的，直接覆盖成false
     *   后面用接口来统一优化
     */
    protected open fun needSupportReback(): Boolean {
        return true
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (isToolbarSupportReback && item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }
}